﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using WeifenLuo.WinFormsUI.Docking;
using CustomUIControls;


namespace SalesVMT
{
    public partial class frmCustomerDebitList : DockContent
    {
        int pageSize = UIParamater.PageSize;
        int currPage = 0;
        int itemCount = 0;
        int totalPage = 0;
        public frmCustomerDebitList()
        {
            InitializeComponent();
            DisplayFrame();
            UISetting.LoadCustomerFromShipment(ref cboCustomer);
        }

        private void DisplayFrame()
        {
            UISetting.AddColumn(ref dgvList1, "Shipment_ID", "Khóa", "", 1, false, true);
            UISetting.AddColumn(ref dgvList1, "RowNumber", "STT", "C", ColumnWidth.RowNumber, true, true);
            UISetting.AddColumn(ref dgvList1, "Shipment_Code", "Phiếu XK", "", ColumnWidth.Code3, true, true);
            UISetting.AddColumn(ref dgvList1, "Shipment_Date", "Ngày xuất", "C", ColumnWidth.Date, true, true);
            UISetting.AddColumn(ref dgvList1, "Shipment_DebitDays", "Số ngày nợ", "R", 110, true, true);
            UISetting.AddColumn(ref dgvList1, "Shipment_Total", "Tổng cộng", "R", ColumnWidth.TotalPrice, true, true);
            UISetting.AddColumn(ref dgvList1, "Shipment_Pay", "Đã thu", "R", ColumnWidth.Price, true, true);
            UISetting.AddColumn(ref dgvList1, "Remainder", "Còn thu", "R", ColumnWidth.Price, true, true);
        }
        private void LoadData()
        {
            dgvList1.Rows.Clear();
            int CustomerID = Convert.ToInt32(cboCustomer.ComboBox.SelectedValue);
            DataTable _dt = new DataTable();
            _dt = ShipmentDB.GetListCustomerDebit(out itemCount, CustomerID, currPage, pageSize).Tables[0];
            PageIndex();
            int n = _dt.Rows.Count;
            for (int i = 0; i < n; i++)
            {
                dgvList1.Rows.Insert(i, 1);
                dgvList1.Rows[i].Cells["RowNumber"].Value = i + 1;
                dgvList1.Rows[i].Cells["Shipment_ID"].Value = _dt.Rows[i]["Shipment_ID"].ToString();
                dgvList1.Rows[i].Cells["Shipment_Code"].Value = _dt.Rows[i]["Shipment_Code"].ToString();
                dgvList1.Rows[i].Cells["Shipment_Date"].Value = Convert.ToDateTime(_dt.Rows[i]["Shipment_Date"]).ToString("dd/MM/yyyy");
                dgvList1.Rows[i].Cells["Shipment_DebitDays"].Value = _dt.Rows[i]["Shipment_DebitDays"].ToString();
                dgvList1.Rows[i].Cells["Shipment_Total"].Value = Convert.ToDecimal(_dt.Rows[i]["Shipment_Total"]).ToString("#,##0");
                dgvList1.Rows[i].Cells["Shipment_Pay"].Value = Convert.ToDecimal(_dt.Rows[i]["Shipment_Pay"]).ToString("#,##0");
                dgvList1.Rows[i].Cells["Remainder"].Value = Convert.ToDecimal(Convert.ToDecimal(_dt.Rows[i]["Shipment_Total"]) - Convert.ToDecimal(_dt.Rows[i]["Shipment_Pay"])).ToString("#,##0");
            }
        }
        private void PageIndex()
        {
            totalPage = itemCount % pageSize > 0 ? (itemCount / pageSize) + 1 : (itemCount / pageSize);
            if (totalPage == 0)
                lblPageIndex.Text = "0" + "/" + totalPage.ToString();
            else
                lblPageIndex.Text = (currPage + 1).ToString() + "/" + totalPage.ToString();
        }
        private void btnFirst_Click(object sender, EventArgs e)
        {
            currPage = 0;
            LoadData();
        }

        private void btnPrevious_Click(object sender, EventArgs e)
        {
            if (currPage != 0)
                currPage--;
            LoadData();
        }

        private void btnNext_Click(object sender, EventArgs e)
        {
            if (totalPage > 0 && currPage != totalPage - 1)
                currPage++;
            LoadData();
        }

        private void btnLast_Click(object sender, EventArgs e)
        {
            if (totalPage != 0)
                currPage = totalPage - 1;
            LoadData();
        }

        private void cboCustomer_SelectedIndexChanged(object sender, EventArgs e)
        {
            currPage = 0;
            LoadData();
        }

        private void mnuPrint_Click(object sender, EventArgs e)
        {
            if (Convert.ToInt32(cboCustomer.ComboBox.SelectedValue) == 0)
                return;
            string CustomerID = cboCustomer.ComboBox.SelectedValue.ToString();
            frmReport fReport = new frmReport("CONG NO KH", new string[] { CustomerID, "Công nợ khách hàng" });
            fReport.Show(this);
        }
    }
}